package com.lex.newbasketball.testhandler;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Message;
import android.support.annotation.Nullable;
import android.os.Handler;
import android.view.View;
import android.widget.Button;

import com.lex.newbasketball.R;
import com.sam.ui.base.activity.BaseActivity;

public class TestHandlerActivity extends BaseActivity {
    private Button button;
    private Handler handler;

    @Override
    protected void onCreate(@Nullable Intent intent) {
        setContentView(R.layout.test_activity_handler);

    }

    @Override
    protected void findView() {
        button = (Button) findViewById(R.id.button);
    }

    @Override
    protected void initView() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MyThread().start();
            }
        });
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 1){
                    button.setText((String) msg.obj);
                }
            }
        };
    }

    class MyThread extends Thread {
        @Override
        public void run() {
            for (int i = 10; i > 0; i--){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Message msg = new Message();
                msg.what = 1;
                msg.obj = i + "秒";
                handler.sendMessage(msg);

                handler.obtainMessage(1, i + "秒").sendToTarget();
            }
        }
    }

    class As extends AsyncTask<A,B,C>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected C doInBackground(A... params) {
            return null;
        }

        @Override
        protected void onPostExecute(C c) {
            super.onPostExecute(c);
        }

        @Override
        protected void onProgressUpdate(B... values) {
            super.onProgressUpdate(values);
        }
    }
}
